#pragma once
//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include <string>
#include <vector>
#include <stdio.h>
#include "VxDefs.h"

class VxKey;

RCODE VxGetCurrentWorkingDirectory( std::string strRetDir );

RCODE VxSetCurrentWorkingDirectory( const char * pDir );

//! returns file size or 0 if doesnt exist
U64		VxFileExists( const char * pFileName );

//! returns file size or 0 if doesnt exist
inline U64		VxGetFileLen( const char * pFileName ){ return VxFileExists(pFileName);}

//! return true if directory exists
bool	VxDirectoryExists( const char * pDir );

//! Make all directories that don't exist in a given path
RCODE	VxMakeDirectory( const char * pDirectoryPath );
//! Make all directories that don't exist in a given path
inline RCODE	VxMakeDirectory( std::string& strDirectoryPath )	{return VxMakeDirectory( strDirectoryPath.c_str());}

//! read a line from file into buffer and null terminate it
RCODE	VxReadLine( FILE *pgFile, char *pBuf, int iBufLen );

//! open a file and log error message if fails
FILE *	VxFileOpen( const char * pFileName, const char * pFileMode );

//! File seek..NOTE: only seeks from beginning of file
RCODE	VxFileSeek ( FILE * poFile, U32 u32Pos );

//! File seek..NOTE: only seeks from beginning of file
RCODE	VxFileSeek ( FILE * poFile, U64 u64Pos );

//! Copy a file
RCODE	VxCopyFile( const char * pOldPath, const char * pNewPath );

//! Delete a file
RCODE	VxDeleteFile( const char * pFileName );

//! Rename a file
RCODE	VxRenameFile( const char * pFileOldName, const char * pFileNewName );

//! copy files to destination directory then delete the source files
RCODE	VxMoveFiles( char * pDestDir, char * pSrcDir );

//! separate Path and file name into separate strings
RCODE	VxSeperatePathAndFile(	const char *	pFullPath,		// path and file name			
								std::string &	strRetPath,		// return path to file
								std::string &	strRetFile );	// return file name

//! separate Path and file name into separate strings
RCODE	VxSeperatePathAndFile(	std::string &	strFullPath,		// path and file name				
								std::string &	strRetPath,		// return path to file
								std::string &	strRetFile );	// return file name

//! remove the path and return just the file name
void	VxGetJustFileName(		const char *	pFullPath,	// file name may be full or just file name
								std::string&	strRetJustFileName );		// return file name

//! get the . extension of file name
void	VxGetFileExtension(		std::string&	strFileName,	// file name with extension
								std::string&	strRetExt );		// return extension ( ie "myfile.etm" would return etm"
//! flip back slashes into forward slashes
void	VxMakeForwardSlashPath( std::string & csFilePath );
//! flip back slashes into forward slashes
void	VxMakeForwardSlashPath( char * pFilePath );
//! flip forward slashes into back slashes
void	VxMakeBackSlashPath( const std::string & csFilePath );
//! flip forward slashes into back slashes
void	VxMakeBackSlashPath( const std::wstring & csFilePath );

//! return true if last char is '/' else '\\'
bool	VxDoesPathEndWithSlash( const char * pFileName );

//! return true if is a root path like C:\dir or /dir
bool	VxIsFullPath( const char * pFileName );
//! Make full path to execute directory if full path was not specified
//! NOTE: be careful .. assumes pFileName has enough space for full path and file name
void	VxMakeFullPath( char * pFileName );
//! Make full path to given directory if full path was not specified.. make path if does not exist
void	VxMakeFullPath( const char * pShortFileName, const char * pDownloadDir, std::string & strRetPath );
//! Make short FileName.. if pDownloadDir and full path contains pDownloadDir then will be path in that dir else just filename
//! return true if FullFileName contained the download directory
bool 	VxMakeShortFileName( const char * pFullFileName, std::string & strRetShortName, const char * pDownloadDir = NULL );

//! Get execution full path
RCODE	VxGetExecuteFullPathAndName( std::string& strRetExePathAndFileName );
//! Get directory we execute from
RCODE	VxGetExecuteDirectory( std::string& strRetExeDir );
//! Get execution path and file name
RCODE	VxGetExecutePathAndName( std::string& strRetExeDir, std::string& strRetExeFileName );
//match file names using dos style wild chars
BOOL	VxFileNameWildMatch(const char  * pMatchName, const char * pWildName);
//! read whole file of known length into existing buffer
//! NOTE assumes buffer has enough room for the whole file
RCODE	VxReadWholeFile(const char *	pFileName,					// file to read
						void *			pvBuf,						// buffer to read into
						U32				u32LenToRead,				// length to read ( assumes is same as file length
						U32	*			pu32RetAmountRead = NULL );	// return length actually read if not null
//! allocate memory and read whole file into memory
//! NOTE: USER MUST DELETE THE RETURED POINTER OR MEMORY LEAK WILL OCCURE
RCODE	VxReadWholeFile(	const char *	pFileName,			// file to read	
							void **			ppvRetBuf,			// return allocated buffer it was read into
							U32 *			pu32RetLenOfFile );	// return length of file
//! allocate memory and read whole file into memory and decrypt
//! NOTE: USER MUST DELETE THE RETURED POINTER OR MEMORY LEAK WILL OCCURE
RCODE	VxReadWholeFile(	VxKey *			poKey,				// key to decrypt with
							const char *	pFileName,			// file to read	
							void **			ppvRetBuf,			// return allocated buffer it was read into
							U32 *			pu32RetLenOfFile );	// return length of file

//! write all of data to a file
RCODE	VxWriteWholeFile(	const char *	pFileName,			// file to write to
							void *			pvBuf,				// data to write
							U32				u32LenOfData );		// data length
//! encrypt and write all of data to a file
RCODE	VxWriteWholeFile(	VxKey *			poKey,				// key to encrypt with
							 const char *	pFileName,			// file to write to
							 void *			pvBuf,				// data to write
							 U32			u32LenOfData );		// data length

//! determine file type
U8	VxFileExtensionToFileTypeFlag( const char *	pFileExt );

